﻿using System.Collections.Generic;
using System.Linq;

namespace Inscriptionem.Models.Blog.Views.Controls
{
    public class ArchiveMenuViewModel : Base.BaseRequestModel
    {
        private ArchiveMenuViewModel()
        {
            ArchiveKeys = new List<int>();
        }

        public ArchiveMenuViewModel(Base.BaseViewModel model)
            : this()
        {
            Language = model.Language;
            Section = model.Section;

            if (model is CategoryViewModel)
            {
                var cmodel = model as CategoryViewModel;

                ArchiveKeys.AddRange(cmodel.Category.PostCategories.Select(pc => pc.Post).OrderByDescending(p => p.CreatedDate)
                                         .GroupBy(p => p.CreatedDate.Year * 100 + p.CreatedDate.Month).Select(k => k.Key));
            }
            else
            {
                ArchiveKeys.AddRange(model.Categories.SelectMany(c => c.PostCategories).Select(pc => pc.Post).OrderByDescending(p => p.CreatedDate)
                                         .GroupBy(p => p.CreatedDate.Year * 100 + p.CreatedDate.Month).Select(k => k.Key));
            }
        }

        public List<int> ArchiveKeys { get; protected set; }
    }
}